{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第三节 定积分的换元法和分部积分法(页码246-247)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由上节结果知道，计算定积分$ \\int_{a}^{b} f(x) \\, \\mathrm{d}x $的简便方法是把$y = f(x)$ 转化为求的原函数的增量.在第四章中，我们知道用换元积分法和分部积分法可以求出一些函数的原函数.因此，在一定条件下，可以用换元积分法和分部积分法来计算定积分.下面就来讨论定积分的这两种计算方法."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、定积分的换元法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为了说明如何用换元法来计算定积分，先证明下面的定理：\n",
    "定理 $y = f(x)$在区间$[a, b]$上连续，函数$y =\\varphi(x)$ 满足条件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**(1)$\\varphi(α)$=a,$\\varphi(β)$=b;**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**(2)$\\varphi(t)$在$[α, β]$(或$[β,α]$)上有连续导数，且值域$R$=$[a,b]$,**\n",
    "则有"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**$ \\int_{a}^{b} f(x) \\, \\mathrm{d}x $=$\\int_α^β f[\\varphi(x)]\\varphi'(x) \\, dx$**                     (3-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "公式(3-1)叫做定积分的换元公式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "证：由假设可以知道，上式两边的被积函数都是连续的，因此不仅上式两边的定积分都存在，而且由上节的定理2知道，被积函数的原函数也都存在。所以，(3-1)式两边的定积分都可应用牛顿-莱布尼兹公式。假设$F(x)$是$f(x)$的一个原函数，则"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**$ \\int_{a}^{b} f(x) \\, \\mathrm{d}x $=$F(b)$-$F(a)$**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "另一方面，记$\\Phi(t)$=$F(φ(x))$,它是由$F(x)$与$x =\\varphi(t)$复合而成的函数，由复合函数求导法则，得"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\Phi'(t)$=$\\mathrm{d}F$/$\\mathrm{d}x$*$\\mathrm{d}x$/$\\mathrm{d}t$=$f(x)$*$\\varphi'(t)$=$f[\\varphi(t)]\\varphi'(t)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这表明$\\Phi(t)$是$f[\\varphi(t)]\\varphi'(t)$的一个原函数，因此有"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\int_α^β f[\\varphi(t)]\\varphi'(t) \\, dt$  =$\\Phi(β)$-$\\Phi(α)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "又由$\\Phi(t)$=$F(φ(t))$及$\\varphi(α)$=a,$\\varphi(β)$=b 可知"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\Phi(β)$-$\\Phi(α)$=$F(φ(β))$-$F(φ(α))$=$F(b)$-$F(a)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "所以"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**$ \\int_{a}^{b} f(x) \\, \\mathrm{d}x $=$F(b)$-$F(a)$=$\\Phi(β)$-$\\Phi(α)$=$\\int_α^β f[\\varphi(t)]\\varphi'(t) \\, dt$** "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这就证明了换元公式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在定积分$ \\int_{a}^{b} f(x) \\, \\mathrm{d}x $中的dx本来是整个定积分记号中不可分期的一部分，但由上述定理可知，在一定条件下，它确实可以作为微分记号来对待.这就是说，利用换元公式时，如果把$ \\int_{a}^{b} f(x) \\, \\mathrm{d}x $中的x换成$\\varphi'(t)$这正好是$x =\\varphi(t)$的微分dx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**应用换元公式时有两点值得注意：(1)用$x =\\varphi(t)$把原来变量x代换成新变量t时，积分限也要换成相应的新变量t的积分限；(2)求出$f[\\varphi(t)]\\varphi'(t)$的一个原函数$\\Phi(t)$后，不必像计算不定积分那样要把$\\Phi(t)$变换成原来变量x的函数，而只要把新变量t的上、下限分别代入$\\Phi(t)$中相减就行了。**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例1：计算 $\\int_{0}^{a} \\sqrt{a^2 - x^2} \\, dx \\quad (a > 0)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.integrate import quad\n",
    "\n",
    "# 定义被积函数\n",
    "def integrand(x, a):\n",
    "    return np.sqrt(a**2 - x**2)\n",
    "\n",
    "# 设置a的值\n",
    "a = 1  \n",
    "\n",
    "# 计算定积分\n",
    "result, error = quad(integrand, 0, a, args=(a,))\n",
    "\n",
    "# 输出结果\n",
    "print(f\"定积分 ∫ 0 a √(a²-x²) dx 的结果是：{result}\")\n",
    "print(f\"估计的误差是：{error}\")\n",
    "\n",
    "# 验证结果（使用圆的面积公式）\n",
    "expected_result = np.pi * a**2 / 4  # 因为只计算了第一象限的面积，所以是圆的面积的1/4\n",
    "print(f\"根据圆的面积公式，第一象限的面积应该是：{expected_result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例2：计算 $ \\int_{0}^{\\frac{\\pi}{2}} \\cos^5(x)\\sin(x) \\, dx $"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义符号变量\n",
    "x = sp.symbols('x')\n",
    "\n",
    "# 定义被积函数\n",
    "integrand = sp.cos(x)**5 * sp.sin(x)\n",
    "\n",
    "# 计算定积分\n",
    "integral_value = sp.integrate(integrand, (x, 0, sp.pi/2))\n",
    "\n",
    "# 输出结果\n",
    "print(f\"定积分 ∫_0^(π/2) cos^5(x)sin(x)dx 的结果是：{integral_value}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上为课本的高数笔记，以下为本章节其他经典例题解."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例题1： 计算$\\int_{0}^{2\\pi} \\cos^2(x) \\, dx$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义符号变量\n",
    "x = sp.symbols('x')\n",
    "\n",
    "# 定义被积函数\n",
    "integrand = sp.cos(x)**2\n",
    "\n",
    "# 计算定积分\n",
    "integral_value = sp.integrate(integrand, (x, 0, 2*sp.pi))\n",
    "\n",
    "# 输出结果\n",
    "print(f\"定积分 ∫_0^(2π) cos^2(x)dx 的结果是：{integral_value}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例题2：计算$\\int_{0}^{1} \\frac{1}{\\sqrt{1-x^2}} \\, dx$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义符号变量\n",
    "t = sp.symbols('t')\n",
    "\n",
    "x = sp.sin(t)\n",
    "dx = sp.cos(t) * sp.diff(t)  \n",
    "integrand = 1 / sp.sqrt(1 - x**2) * dx.subs(x, sp.sin(t))  \n",
    "\n",
    "integral_value = sp.integrate(1, (t, 0, sp.pi/2))\n",
    "\n",
    "# 输出结果\n",
    "print(f\"定积分 ∫_0^1 1/√(1-x^2) dx 的结果是（通过换元法计算）：{integral_value}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例题3：计算 $\\int_{0}^{e} x \\ln(x) \\, dx$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义符号变量\n",
    "x = sp.symbols('x')\n",
    "\n",
    "# 定义被积函数\n",
    "integrand = x * sp.log(x)\n",
    "\n",
    "# 计算定积分\n",
    "integral_value = sp.integrate(integrand, (x, 0, sp.E))\n",
    "\n",
    "# 输出结果\n",
    "print(f\"定积分 ∫_0^e x ln(x) dx 的结果是：{integral_value}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例题4：计算 $\\int_{0}^{3} \\frac{1 + x}{2} \\, x \\, dx$ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义符号变量\n",
    "x = sp.symbols('x')\n",
    "\n",
    "# 定义被积函数\n",
    "integrand = (1 + x) * x / 2\n",
    "\n",
    "# 计算定积分\n",
    "integral_value = sp.integrate(integrand, (x, 0, 3))\n",
    "\n",
    "# 输出结果\n",
    "print(f\"定积分 ∫_0^3 (1 + x)x/2 dx 的结果是：{integral_value}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.14.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
